home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / xml4j.jar / com / ibm / xml / parser / Parent.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-08-30  |  4.4 KB  |  259 lines

  1. package com.ibm.xml.parser;
  2.  
  3. import java.util.Enumeration;
  4. import org.w3c.dom.DOMException;
  5. import org.w3c.dom.DocumentFragment;
  6. import org.w3c.dom.EntityReference;
  7. import org.w3c.dom.Node;
  8. import org.w3c.dom.NodeList;
  9.  
  10. public abstract class Parent extends Child {
  11.    static final long serialVersionUID = -5379012622242611200L;
  12.    TXNodeList children = new TXNodeList();
  13.  
  14.    public NodeList getChildNodes() {
  15.       return this.children;
  16.    }
  17.  
  18.    public boolean hasChildNodes() {
  19.       return this.children.getLength() > 0;
  20.    }
  21.  
  22.    public Enumeration elements() {
  23.       return this.children.elements();
  24.    }
  25.  
  26.    public Child[] getChildrenArray() {
  27.       Child[] var1 = new Child[this.children.getLength()];
  28.       this.children.nodes.copyInto(var1);
  29.       return var1;
  30.    }
  31.  
  32.    public Node getFirstChild() {
  33.       return this.children.getLength() > 0 ? this.children.item(0) : null;
  34.    }
  35.  
  36.    public Node getFirstWithoutReference() {
  37.       Node var1;
  38.       Node var2;
  39.       for(var1 = this.getFirstChild(); var1 != null && var1.getNodeType() == 5; var1 = var2) {
  40.          var2 = var1.getFirstChild();
  41.          if (var2 == null) {
  42.             var1 = ((Child)var1).getNextWithoutReference();
  43.             break;
  44.          }
  45.       }
  46.  
  47.       return var1;
  48.    }
  49.  
  50.    public Node getLastChild() {
  51.       int var1 = this.children.getLength();
  52.       return var1 > 0 ? this.children.item(var1 - 1) : null;
  53.    }
  54.  
  55.    public Node getLastWithoutReference() {
  56.       Node var1;
  57.       Node var2;
  58.       for(var1 = this.getLastChild(); var1 != null && var1.getNodeType() == 5; var1 = var2) {
  59.          var2 = var1.getLastChild();
  60.          if (var2 == null) {
  61.             var1 = ((Child)var1).getPreviousWithoutReference();
  62.             break;
  63.          }
  64.       }
  65.  
  66.       return var1;
  67.    }
  68.  
  69.    protected abstract void checkChildType(Node var1) throws DOMException;
  70.  
  71.    protected void realInsert(Node var1, int var2) throws DOMException {
  72.       if (var1.getParentNode() != null) {
  73.          var1.getParentNode().removeChild(var1);
  74.       }
  75.  
  76.       this.checkChildType(var1);
  77.       if (var1 == this) {
  78.          throw new TXDOMException((short)3, "Can't have itself as child.");
  79.       } else {
  80.          TXDocument var3 = ((Child)this).getFactory();
  81.          if (var3 != null) {
  82.             if (var3.isCheckOwnerDocument() && var3 != var1.getOwnerDocument()) {
  83.                throw new TXDOMException((short)4, "Specified child was created from a different document. The parent is \"" + ((Child)this).getNodeName() + "\", the child is \"" + var1.getNodeName() + "\".");
  84.             }
  85.  
  86.             if (var3.isCheckNodeLoop()) {
  87.                Object var4 = this;
  88.  
  89.                while((var4 = ((Node)var4).getParentNode()) != null) {
  90.                   if (var4 == var1) {
  91.                      throw new TXDOMException((short)3, "Can't have an ancestor as child");
  92.                   }
  93.                }
  94.             }
  95.          }
  96.  
  97.          this.children.insert(var2, var1);
  98.          ((Child)var1).setParentNode(this);
  99.          ((Child)this).clearDigest();
  100.       }
  101.    }
  102.  
  103.    public synchronized void insert(Node var1, int var2) throws DOMException {
  104.       if (!(var1 instanceof DocumentFragment)) {
  105.          this.realInsert(var1, var2);
  106.       } else {
  107.          Node var3;
  108.          while((var3 = var1.getLastChild()) != null) {
  109.             var1.removeChild(var3);
  110.             this.insert(var3, var2);
  111.          }
  112.  
  113.       }
  114.    }
  115.  
  116.    public synchronized Node insertBefore(Node var1, Node var2) throws DOMException {
  117.       if (var2 == null) {
  118.          this.insert(var1, this.children.getLength());
  119.       } else {
  120.          int var3 = this.children.indexOf(var2);
  121.          if (var3 < 0) {
  122.             throw new TXDOMException((short)8, "com.ibm.xml.parser.Parent#insertBefore(): Node " + var2 + " is not found in the child list.");
  123.          }
  124.  
  125.          this.insert(var1, var3);
  126.       }
  127.  
  128.       return var1;
  129.    }
  130.  
  131.    public synchronized Node insertAfter(Node var1, Node var2) throws DOMException {
  132.       if (var2 == null) {
  133.          this.insert(var1, 0);
  134.       } else {
  135.          this.insertBefore(var1, var2.getNextSibling());
  136.       }
  137.  
  138.       return var1;
  139.    }
  140.  
  141.    public synchronized Node insertFirst(Node var1) {
  142.       this.insert(var1, 0);
  143.       return var1;
  144.    }
  145.  
  146.    public synchronized Node insertLast(Node var1) {
  147.       this.insert(var1, this.children.getLength());
  148.       return var1;
  149.    }
  150.  
  151.    /** @deprecated */
  152.    public synchronized void addElement(Child var1) {
  153.       if (var1 != null) {
  154.          this.insert(var1, this.children.getLength());
  155.       }
  156.  
  157.    }
  158.  
  159.    public synchronized Node appendChild(Node var1) {
  160.       if (var1 != null) {
  161.          this.insert(var1, this.children.getLength());
  162.       }
  163.  
  164.       return var1;
  165.    }
  166.  
  167.    public synchronized Node replaceChild(Node var1, Node var2) throws DOMException {
  168.       int var3 = this.children.indexOf(var2);
  169.       if (var3 < 0) {
  170.          throw new TXDOMException((short)8, "com.ibm.xml.parser.Parent#replaceChild(): Node " + var2 + " is not found in the child list.");
  171.       } else if (var1 == var2) {
  172.          return var1;
  173.       } else {
  174.          if (var1.getParentNode() != null) {
  175.             var1.getParentNode().removeChild(var1);
  176.          }
  177.  
  178.          Child var4 = (Child)var1;
  179.          this.children.replace(var3, var4);
  180.          var4.setParentNode(this);
  181.          ((Child)this).clearDigest();
  182.          return var2;
  183.       }
  184.    }
  185.  
  186.    public synchronized Node removeChild(Node var1) throws DOMException {
  187.       int var2 = this.children.indexOf(var1);
  188.       if (var2 < 0) {
  189.          throw new TXDOMException((short)8, "com.ibm.xml.parser.Parent#removeChild(): Node " + var1 + " is not found in the child list.");
  190.       } else {
  191.          this.children.remove(var2);
  192.          ((Child)this).clearDigest();
  193.          return var1;
  194.       }
  195.    }
  196.  
  197.    protected void processAfterRemove(Node var1) {
  198.       short var2 = var1.getNodeType();
  199.       if (var2 == 1) {
  200.          ((TXElement)var1).collectNamespaceAttributes(this);
  201.       } else {
  202.          if (var2 == 5) {
  203.             ((GeneralReference)var1).collectNamespaceAttributes(this);
  204.          }
  205.  
  206.       }
  207.    }
  208.  
  209.    public void expandEntityReferences() {
  210.       Node var1 = this.getFirstChild();
  211.  
  212.       while(var1 != null) {
  213.          if (var1 instanceof Parent) {
  214.             ((Parent)var1).expandEntityReferences();
  215.             if (var1 instanceof EntityReference) {
  216.                Node var2;
  217.                while((var2 = var1.getLastChild()) != null) {
  218.                   var1.removeChild(var2);
  219.                   this.insertAfter(var2, var1);
  220.                }
  221.  
  222.                var2 = var1.getNextSibling();
  223.                this.removeChild(var1);
  224.                var1 = var2;
  225.                continue;
  226.             }
  227.          }
  228.  
  229.          var1 = var1.getNextSibling();
  230.       }
  231.  
  232.    }
  233.  
  234.    public String getText() {
  235.       int var1;
  236.       if (this.children != null && (var1 = this.children.getLength()) != 0) {
  237.          if (var1 == 1) {
  238.             return ((Child)this.children.item(0)).getText();
  239.          } else {
  240.             StringBuffer var2 = new StringBuffer(128);
  241.             TXNodeList var3 = this.children;
  242.             synchronized(var3){}
  243.  
  244.             try {
  245.                for(int var5 = 0; var5 < var1; ++var5) {
  246.                   var2.append(((Child)this.children.item(var5)).getText());
  247.                }
  248.             } catch (Throwable var7) {
  249.                throw var7;
  250.             }
  251.  
  252.             return var2.toString().intern();
  253.          }
  254.       } else {
  255.          return "";
  256.       }
  257.    }
  258. }
  259.